import {
  PLATFORM
} from '@/store/mutation-types'
import storage from '@/utils/storage'

import * as UserApi from '@/api/user'

const app = {
  state: {
    // 当前终端平台
    platform: '',
    device: {
      top: 0,
      bottom: 0,
    },
    store: '',
    storeJSON: {},
    cateId: 0,
    serviceMoveY: 0,
  },

  mutations: {
    SET_PLATFORM: (state, value) => {
      state.platform = value
    },
    SET_STORE: (state, value) => {
      state.store = value
    },
    SET_DEVICE: (state, value) => {
      state.device = value
    },
    SET_CATE_ID: (state, value) => {
      state.cateId = value
    },
    SET_STORE_JSON: (state, value) => {
      state.storeJSON = value
    },
    SET_SERVICEMOVEY: (state, value) => {
      state.serviceMoveY = value || 0
    }
  },

  actions: {
    SetStore({
      commit
    }, data) {
      commit('SET_STORE', data)
      storage.set('DR_SELECTED_STORE', data)
      const store = this
      if (store.state.user.token) {
        UserApi.nearbyStore({
            role_id: data.role_id,
            userId: store.state.user.userId
          })
          .then(res => {
            console.log(res)
          })
      }
    },
    SetCateId({
      commit
    }, data) {
      commit('SET_CATE_ID', data)
    },
    SetStoreJSON({
      commit
    }, data) {
      let obj = {}
      data.map(item => {
        obj[item.role_id] = item
      })
      commit('SET_STORE_JSON', obj)
    },
    setServiceMoveY({commit}, data) {
      commit('SET_SERVICEMOVEY', data)
    }
  }
}

export default app
